<?php
namespace app\admin\controller;

class User extends Base
{
	 public function user_group(){
        $test=model('TkUserGrop')->select();
        $rank=[];
        $gname=[];
        $gid=[];
        foreach ($test as $k=>$v) {
            $rank[]=$v['rank'];
            $gname[]=$v['gname'];
            $gid[]=$v['gid'];
        }
        $this->assign("gid",$gid);
        $this->assign("rank",$rank);
        $this->assign("gname",$gname);
        return $this->fetch();
    }
    public function user(){
        if(!input("param.id")){
            $tk=model('TkUser');
            $ts=$tk->alias("t")->join("tk_user_grop m","t.gid=m.gid","left")->order("t.id desc")->paginate('5',false);
            $page=$ts->render();
            $status=[];
            foreach ($ts as $k=>$v) {
                $status[]=$v->toArray();
            }  
            $uname=[];
            $auth=[];
            $role=[];
            $id=[];
            $gid=[];
           foreach ($status as $k => $v) {
               $uname[]=$v['uname'];
               $auth[]=$v['statu'];
               $role[]=$v['gname'];
               $id[]=$v['id'];
               $gid[]=$v['gid'];
           }
           $this->assign("gid",$gid);
           $this->assign("id",$id);
           $this->assign("page",$page);
           $this->assign("uname",$uname);
           $this->assign("auth",$auth);
           $this->assign("role",$role);
            return $this->fetch();
        }else{
            $tk=model('TkUser');
            $ts=$tk->alias("t")->join("tk_user_grop m","t.gid=m.gid","left")->where(["t.gid"=>input("param.id")])->order("t.id desc")->paginate('5',false  );
            $page=$ts->render();
            $status=[];
            foreach ($ts as $k=>$v) {
                $status[]=$v->toArray();
            }  
            $uname=[];
            $auth=[];
            $role=[];
            $id=[];
            $gid=[];
           foreach ($status as $k => $v) {
               $uname[]=$v['uname'];
               $auth[]=$v['statu'];
               $role[]=$v['gname'];
               $id[]=$v['id'];
               $gid[]=$v['gid'];
           }
           $this->assign("gid",$gid);
           $this->assign("id",$id);
           $this->assign("page",$page);
           $this->assign("uname",$uname);
           $this->assign("auth",$auth);
           $this->assign("role",$role);
            return $this->fetch(); 
        }
       
    }
     public function add_user(){
        $tk=model('TkUserGrop');
        $ts=$tk->select();
        $ip=request()->ip();
        $date=date("Y-m-d H:i:s");
        $status=[];
        foreach ($ts as $k=>$v) {
            $status[]=$v->toArray();
        } 
        $role=[]; 
        $id=[];
        foreach ($status as $k => $v) {
           $role[]=$v['gname'];
           $id[]=$v['gid'];
       }
        if(input("post.sub")){
          $users=input("post.users");
          $table=model("TkUser");
          $num=$table->where(["uname"=>$users])->count();
          if(!input("post.users") || !input("post.pwd")){
            return $this->msg("用户名和密码不能为空","/admin/user/add_user");
            exit;
          }
          if($num==1){
            return $this->msg("用户名重名","/admin/user/add_user");  
          }else{
            $pwd=md5(input("post.pwd"));
            $role=input("post.grop");
            $where=[
                'uname'=>$users,
                'pwd'=>$pwd,
                'gid'=>$role
            ];
            $result=$table->data($where)->save();
            if($result==1){
                 return $this->msg("添加成功","/admin/user/user");
            }
          }
        }
        $this->assign("id",$id);
        $this->assign("ip",$ip);
        $this->assign("role",$role);
        $this->assign("date",$date);
        return $this->fetch();
    }
    public function delete(){
        $id=input("param.id");
        $tab=model("TkUser");
        $result=$tab->where(["id"=>$id])->delete();
        if($result==1){
            return $this->msg("删除成功","/admin/user/user");
        }else{

            return $this->msg("删除失败","/admin/user/user");

        }
    }
    //修改是否禁用状态 1是启用 0是禁用
    public function status(){
      if(input("param.")){
        $status=input("param.ifstatus");
        $id=input("param.id");
        if($status==1){
          $model=model("TkUser");
          $result=$model->save(['statu'=>$status],['id'=>$id]);
          if($result==1){
            $this->msg("修改成功","/admin/user/user");
           }
        }else{
          $status=input("param.ifstatus");
          $id=input("param.id");
          if($status==0){
            $model=model("TkUser");
            $result=$model->save(['statu'=>$status],['id'=>$id]);
            if($result==1){
              $this->msg("修改成功","/admin/user/user");
             }
        }
      }
    }
  }
    //修改管理员账号
    public function edit(){
        if(input("post.sub")){
            if(!input("post.uname") || !input("post.pwd")){
                return $this->msg("用户名和密码不能为空","/admin/user/add_user");
                exit;
            }
            $gid=input("param.id"); 
            $pwd=md5($_POST['pwd']);
            request()->post(['pwd'=>$pwd]);
            $messege=input("post.");
            $uname=input("post.uname");
            $model=model("TkUser");
            $num=$model->where(['uname'=>$uname])->count();
            if($num==1){
                return $this->msg("用户名重名,修改失败","/admin/user/edit/id/$gid"); 
            }
            $result=$model->allowField(true)->save($messege,['id'=>$gid]);
            if($result==1){
              return $this->msg("修改成功","/admin/user/user");  
          }else{
              return $this->msg("修改失败","/admin/user/edit/id/$id"); 
          }
        }
         $tab=model("TkUserGrop")->select();
            $table=[];
            foreach ($tab as $k=>$v) {
                $table[]=$v->toArray();
            }
            $group=[];
            $id=[];
            foreach ($table as $k=>$v) {
                $id[]=$v['gid'];
                $group[]=$v['gname'];
            }
            $edit=input("param.gid");
            $this->assign("edit",$edit);   
            $this->assign("id",$id);   
            $this->assign("group",$group);
            return $this->fetch();
    }
    public function add_group(){
        $model=model("TkUserGrop");
        $status=$model->select();
        $states=[];
        foreach ($status as $k=>$v) {
               $states[]=$v->toArray();
        }
        $rank=[];
        foreach ($states as $k=>$v) {
          $rank[]=$v['rank'];
        }
        $rank=implode(",",$rank);
        $this->assign("rank",$rank);
        return $this->fetch();
    }
    public function msg($message,$url){
     echo "<script>alert('{$message}');location.href='{$url}'</script>";
    }
}